'use strict';

const Controller = require('egg').Controller;

class CarController extends Controller {
    //添加
    async create() {
        let {ctx,service} = this;
        let {product_id,num=1} = ctx.request.body;
        let user_id = ctx.request.user_id;
        console.log(user_id,"user_id")
        try{
            ctx.validate({product_id:'string'});

            try{

                //1.查 user_id product_id 

                let target = await service.car.selectProduct(ctx.request.user_id,product_id)
                if(!target.length){
                    //添加
                    await service.car.create(user_id,product_id,num);
                    
                }else{
                    //修改数量
                    await service.car.update(user_id,product_id,target[0].num+1);
                }

                ctx.body = {
                    code:1,
                    msg:'成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'失败'
                }
            }

            

        }catch(e){
            ctx.body = {
                code:2,
                msg:e
            }
        }
    }
    //删除
    async destroy() {
        let {ctx,service} = this;
        let {id} = ctx.params;
        if(id){
            try{
                await service.car.destroy(id)
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    } 
    //更新  /api/car/17
    async update(){
        let {ctx,service} = this;
        let {id} = ctx.params;
        let {num} = ctx.request.body;
        if(id && num){
            try{
                await service.car.carUpdate(id,num);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'失败'
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查找
    async index(){
        let {ctx,service} = this;
        let list = await service.car.index(ctx.request.user_id);
        ctx.body = {
            code:1,
            data:list
        }
    }

    //全部删除
    async allDel(){
        let {ctx,service} = this;
        let {ids} = ctx.request.query;

        if(ids){
            try{
                await service.car.delAll(ids);
                ctx.body = {
                    code:1,
                    msg:'清空成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = CarController;
